// MySQL version
#pragma once

#include "../comm/log.hpp"
#include "../comm/util.hpp"

#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <vector>
#include <fstream>
#include <stdlib.h>

#include "include/mysql.h"

// 根据题目list文件，加载所有题目信息到内存中
// model: 只要用来和数据进行交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    class Question
    {
    public:
        std::string number; // 题目编号
        std::string title;  // 题目标题
        std::string star;   // 题目难度 简单 中等 困难
        std::string desc;   // 题目的描述
        std::string header; // 题目预设给用户在线编辑器的代码
        std::string tail;   // 题目的测试用例，和header拼接形成完整代码
        int cpu_limit;      // 题目的时间要求(S)
        int mem_limit;      // 题目的空间要求(KB)
    };

    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;
    class Model
    {
    public:
        Model()
        {}
        bool QueryMySql(const std::string &sql, std::vector<Question> *out)
        {
            MYSQL *my = mysql_init(nullptr);
            if(!mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败 ..." << "\n";
                return false;
            }
            LOG(INFO) << "连接数据库成功 ..." << "\n";
            
            if(mysql_query(my, sql.c_str()) != 0)
            {
                LOG("WARNING") << sql << " execute error ..." << "\n";
                return false;
            }
            
            MYSQL_RES *result = mysql_store_result(my);
            int rows = mysql_num_rows(result);
            int fields = mysql_num_fields(result);
            Question q;
            for(int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(result);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);
                out->push_back(q);
            }

            mysql_free_result(result);
            mysql_close(my);
            return true;
        }
        bool GetAllQuestions(std::vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }

        bool GetOneQuestion(const std::string &number, Question *q)
        {
            bool ret = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            std::vector<Question> result;
            if(QueryMySql(sql, &result))
            {
                if(result.size() == 1)
                {
                    *q = result[0];
                    ret = true;
                }
            }
            return ret;
        }

        ~Model()
        {
        }
    };
}